using System;
using BMCommon;
using Holoville.HOTween;
using UnityEngine;

public class tk2dUIScrollbarExt : tk2dUIScrollbar
{
	private float SCROLLBAR_WIDTH
	{
		get
		{
			if (this._SCROLLBAR_WIDTH == 0f)
			{
				Camera camera = Scene.FindCameraForObject(base.gameObject);
				this._SCROLLBAR_WIDTH = BMTool.ConvertMillimeterToWorldLength(camera, 0.5f);
			}
			return this._SCROLLBAR_WIDTH;
		}
	}

	private void Awake()
	{
		this.mat = (Resources.Load("autoScrollMat") as Material);
		this.mat.color = new Color(0f, 0f, 0f, 0f);
	}

	private void Update()
	{
		if (this.rawPercent < -0.0001f)
		{
			this.ResizeThumb(this.rawPercent);
		}
		else if (this.rawPercent > 1.0001f)
		{
			this.ResizeThumb(this.rawPercent - 1f);
		}
	}

	public void InitScrollBar(float visibleAreaLength, float contentLength)
	{
		this.SetBarData(visibleAreaLength, contentLength);
		this.CreateThumb();
	}

	public void UpdateScrollBar(float visibleAreaLength, float contentLength)
	{
		this.SetBarData(visibleAreaLength, contentLength);
		this.UpdateScrollBarAppearance();
	}

	private void SetBarData(float visibleAreaLength, float contentLength)
	{
		this.scrollBarLength = visibleAreaLength;
		if (contentLength != 0f)
		{
			this.thumbLength = visibleAreaLength / contentLength * visibleAreaLength;
		}
		else
		{
			this.thumbLength = 0f;
		}
		Camera camera = Scene.FindCameraForObject(base.gameObject);
		if (camera == null)
		{
			camera = Camera.main;
		}
		if (camera == null)
		{
			UnityEngine.Debug.LogError("tk2dUIScrollableArea.CalculateContentAndVisibleLength : Camera not found");
			return;
		}
		Vector3 position = camera.ViewportToWorldPoint(new Vector3(1f, 0f));
		position.x -= this.SCROLLBAR_WIDTH * 2f;
		base.transform.position = position;
		this.rightOffsetX = base.transform.localPosition.x;
		base.transform.localPosition = Vector2.right * this.rightOffsetX;
		this.thumbImgLength = Mathf.Max(this.scrollBarLength * 0.1f, this.thumbLength);
		this.thumbImgLength -= this.thumbImgLength * 0.05f;
	}

	public void AnimateShow()
	{
		TweenParms tweenParms = new TweenParms();
		tweenParms.Prop("matColor", new Color(0f, 0f, 0f, 0.5f)).Id(base.gameObject.name).Ease(EaseType.Linear).OnUpdate(delegate()
		{
			this.mat.color = this.matColor;
		});
		HOTween.To(this, 0.2f, tweenParms);
	}

	public void AnimateHide()
	{
		TweenParms tweenParms = new TweenParms();
		tweenParms.Prop("matColor", new Color(0f, 0f, 0f, 0f)).Id(base.gameObject.name).Delay(0.35f).Ease(EaseType.Linear).OnUpdate(delegate()
		{
			this.mat.color = this.matColor;
		});
		HOTween.To(this, 0.3f, tweenParms);
	}

	private void CreateThumb()
	{
		if (base.transform.childCount > 0)
		{
			return;
		}
		this.thumb = new GameObject("Thumb");
		this.thumb.transform.parent = base.transform;
		this.thumb.transform.localPosition = Vector3.zero;
		this.thumbTransform = this.thumb.transform;
		this.pivot = new GameObject("Pivot");
		this.pivot.transform.parent = this.thumb.transform;
		this.pivot.transform.localPosition = Vector3.zero;
		this.center = GameObject.CreatePrimitive(PrimitiveType.Cube);
		this.top = GameObject.CreatePrimitive(PrimitiveType.Cube);
		this.bot = GameObject.CreatePrimitive(PrimitiveType.Cube);
		this.mat.SetColor("Main Color", new Color(0f, 0f, 0f, 0f));
		this.center.GetComponent<Renderer>().material = this.mat;
		this.top.GetComponent<Renderer>().material = this.mat;
		this.bot.GetComponent<Renderer>().material = this.mat;
		this.center.transform.parent = this.pivot.transform;
		this.top.transform.parent = this.pivot.transform;
		this.bot.transform.parent = this.pivot.transform;
		this.UpdateScrollBarAppearance();
	}

	private void UpdateScrollBarAppearance()
	{
		this.center.transform.localScale = new Vector3(this.SCROLLBAR_WIDTH, this.thumbImgLength, 1f);
		this.top.transform.localScale = Vector3.one * this.SCROLLBAR_WIDTH;
		this.bot.transform.localScale = Vector3.one * this.SCROLLBAR_WIDTH;
		this.center.transform.localPosition = Vector3.zero;
		Bounds bounds = this.center.GetComponent<Renderer>().bounds;
		this.top.transform.localPosition = new Vector3(bounds.max.x - bounds.center.x - bounds.extents.x, bounds.max.y - bounds.center.y, 0f);
		this.bot.transform.localPosition = new Vector3(bounds.min.x - bounds.center.x + bounds.extents.x, bounds.min.y - bounds.center.y, this.center.transform.localPosition.z);
		Vector3 a = Vector3.down * bounds.extents.y;
		Vector3 b = Vector3.down * (this.thumbImgLength * 0.05f) * 0.5f;
		this.pivot.transform.localPosition = a + b;
		this.pivotPosY = this.pivot.transform.localPosition.y;
	}

	private void UpdateThumbsPosition()
	{
		Bounds bounds = this.center.GetComponent<Renderer>().bounds;
		this.center.transform.localPosition = Vector3.zero;
		this.top.transform.localPosition = new Vector3(bounds.max.x - bounds.extents.x, bounds.max.y, 0f);
		this.bot.transform.localPosition = new Vector3(bounds.min.x + bounds.extents.x, bounds.min.y, 0f);
	}

	private void ResizeThumb(float flowAmount)
	{
		float num = this.thumbLength / this.scrollBarLength;
		float num2 = (flowAmount >= 0f) ? -1f : 1f;
		float num3 = this.thumbImgLength + flowAmount / num * num2;
		this.center.transform.localScale = new Vector3(this.SCROLLBAR_WIDTH, num3, 1f);
		this.center.transform.localPosition = Vector3.zero;
		Bounds bounds = this.center.GetComponent<Renderer>().bounds;
		this.top.transform.localPosition = new Vector3(bounds.max.x - bounds.center.x - bounds.extents.x, bounds.max.y - bounds.center.y, 0f);
		this.bot.transform.localPosition = new Vector3(bounds.min.x - bounds.center.x + bounds.extents.x, bounds.min.y - bounds.center.y, this.center.transform.localPosition.z);
		float num4 = (this.thumbImgLength - num3) * 0.5f * num2;
		this.pivot.transform.localPosition = new Vector3(0f, this.pivotPosY + num4, 0f);
	}

	private const float SCROLLBAR_OFFSET_MULIPLE = 0.05f;

	public Color matColor;

	private Material mat;

	private GameObject thumb;

	private GameObject pivot;

	private GameObject center;

	private GameObject top;

	private GameObject bot;

	private float rightOffsetX;

	private float pivotPosY;

	private float thumbImgLength;

	private float _SCROLLBAR_WIDTH;
}
